/**
 * 简单工厂模式
 *      局限性：无法创建多类应用
 * 工厂方法模式
 *      优点：可以创建多个类的实例对象
 *           避免使用者与对象类之间的耦合[用户不必关心创建该对象的具体类，只需调用工厂方法即可]
 */

 /**
  *    Q&A
  */
 /**
  * 通过工厂方法模式为页面构建不同功能的按钮
  * content: the value in the button that displays
  * type:default, primary, warning, danger, disable
  */
var Factory = function(type, content) {
    if (this instanceof Factory) {
        return this[type](content);
    } else {
        return new Factory(content, type);
    }
};

Factory.prototype = {
    default: function(content) {
        this.content = content;
        (function(content) {
            var button = document.createElement('button');
            button.innerText = content;
            button.style.background = 'white';
            button.style.color = 'grey';
            document.getElementById('container').appendChild(button);
        })(content);
    },
    primary: function(content) {
        this.content = content;
        (function(content) {
            var button = document.createElement('button');
            button.innerText = content;
            button.style.background = 'green';
            button.style.color = 'white';            
            document.getElementById('container').appendChild(button);
        })(content);
    },
    warning: function(content) {
        this.content = content;
        (function(content) {
            var button = document.createElement('button');
            button.innerText = content;
            button.style.background = 'yellow';
            button.style.color = 'white';            
            document.getElementById('container').appendChild(button);
        })(content);
    },
    danger: function(content) {
        this.content = content;
        (function(content) {
            var button = document.createElement('button');
            button.innerText = content;
            button.style.background = 'red';
            button.style.color = 'white';            
            document.getElementById('container').appendChild(button);
        })(content);
    },
    disable: function(content) {
        this.content = content;
        (function(content) {
            var button = document.createElement('button');
            button.innerText = content;
            button.style.background = 'grey';
            button.style.color = 'white';  
            button.disabled = true;//button.disabled = 'disabled';
            document.getElementById('container').appendChild(button);
        })(content);
    }
};

var data = [
    {type: 'default', content: 'default'},
    {type: 'primary', content: 'primary'},
    {type: 'warning', content: 'warning'},
    {type: 'danger', content: 'danger'},
    {type: 'disable', content: 'disable'}
];

for(var i in data) {
    Factory(data[i].type, data[i].content);
}
